home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / PowerLisp 2.01 / PowerLisp 2.01 ƒ / examples / eliza.lisp < prev    next >
Lisp/Scheme  |  1995-03-16  |  11KB  |  319 lines

  1. ;;;; -*- Mode: Lisp; Syntax: Common-Lisp -*-
  2. ;;;; Code from Paradigms of AI Programming
  3. ;;;; Copyright (c) 1991 Peter Norvig
  4.  
  5. (in-package "COMMON-LISP-USER")
  6.  
  7. (defconstant fail nil "Indicates pat-match failure")
  8.  
  9. (defconstant no-bindings '((t . t))
  10.   "Indicates pat-match success, with no variables.")
  11.  
  12. (defun starts-with (list x)
  13.     "Is x a list whose first element is x?"
  14.     (and (consp list) (eql (first list) x)))
  15.  
  16. (defun variable-p (x)
  17.     "Is x a variable (a symbol beginning with `?')?"
  18.     (and (symbolp x) (equal (char (symbol-name x) 0) #\?)))
  19.  
  20. (defun get-binding (var bindings)
  21.     "Find a (variable . value) pair in a binding list."
  22.     (assoc var bindings))
  23.  
  24. (defun binding-val (binding)
  25.     "Get the value part of a single binding."
  26.     (cdr binding))
  27.  
  28. (defun match-variable (var input bindings)
  29.     "Does VAR match input?  Uses (or updates) and returns bindings."
  30.     (let ((binding (get-binding var bindings)))
  31.         (cond 
  32.             ((not binding) (extend-bindings var input bindings))
  33.             ((equal input (binding-val binding)) bindings)
  34.             (t fail))))
  35.  
  36. (defun extend-bindings (var val bindings)
  37.     "Add a (var . value) pair to a binding list."
  38.     (cons (cons var val)
  39.         ;; Once we add a "real" binding,
  40.         ;; we can get rid of the dummy no-bindings
  41.         (if (and (eq bindings no-bindings))
  42.             nil
  43.             bindings)))
  44.  
  45. (defun pat-match (pattern input &optional (bindings no-bindings))
  46.     "Match pattern against input in the context of the bindings"
  47.     (cond 
  48.         ((eq bindings fail) fail)
  49.         ((variable-p pattern)
  50.          (match-variable pattern input bindings))
  51.         ((eql pattern input) bindings)
  52.         ((segment-pattern-p pattern)
  53.          (segment-match pattern input bindings))
  54.         ((and (consp pattern) (consp input)) 
  55.          (pat-match (rest pattern) (rest input)
  56.                     (pat-match (first pattern) (first input) 
  57.                                bindings)))
  58.         (t fail)))
  59.  
  60. (defun segment-pattern-p (pattern)
  61.     "Is this a segment matching pattern: ((?* var) . pat)"
  62.     (and (consp pattern)
  63.         (starts-with (first pattern) '?*)))
  64.  
  65. (defun segment-match (pattern input bindings &optional (start 0))
  66.     "Match the segment pattern ((?* var) . pat) against input."
  67.     (let ((var (second (first pattern)))
  68.         (pat (rest pattern)))
  69.     (if (null pat)
  70.         (match-variable var input bindings)
  71.         ;; We assume that pat starts with a constant
  72.         ;; In other words, a pattern can't have 2 consecutive vars
  73.         (let ((pos (position (first pat) input
  74.                              :start start :test #'equal)))
  75.             (if (null pos)
  76.                 fail
  77.                 (let ((b2 (pat-match
  78.                           pat (subseq input pos)
  79.                           (match-variable var (subseq input 0 pos)
  80.                                           bindings))))
  81.                     ;; If this match failed, try another longer one
  82.                     (if (eq b2 fail)
  83.                         (segment-match pattern input bindings (+ pos 1))
  84.                         b2)))))))
  85.  
  86. (defun rule-pattern (rule) (first rule))
  87. (defun rule-responses (rule) (rest rule))
  88.  
  89. (defun use-eliza-rules (input)
  90.     "Find some rule with which to transform the input."
  91.     (if (= (length input) 0)
  92.         (return '(Please type good-bye to quit)))
  93.     (some 
  94.         #'(lambda (rule)
  95.             (let ((result (pat-match (rule-pattern rule) input)))
  96.                 (if (not (eq result fail))
  97.                     (sublis (switch-viewpoint result)
  98.                         (random-elt (rule-responses rule))))))
  99.         *eliza-rules*))
  100.  
  101. (defun switch-viewpoint (words)
  102.     "Change I to you and vice versa, and so on."
  103.     (sublis 
  104.         '((I . you) 
  105.           (you . I) 
  106.           (me . you)
  107.           (my . your)
  108.           (your . my)
  109.           (our . your) 
  110.           (am . are))
  111.           words))
  112.  
  113. ;;; ==============================
  114.  
  115. (defun flatten (the-list)
  116.     "Append together elements (or lists) in the list."
  117.     (mappend #'mklist the-list))
  118.  
  119. (defun mklist (x)
  120.     "Return x if it is a list, otherwise (x)."
  121.     (if (listp x)
  122.         x
  123.         (list x)))
  124.  
  125. (defun mappend (fn the-list)    
  126.     "Apply fn to each element of list and append the results."
  127.     (apply #'append (mapcar fn the-list)))
  128.  
  129. (defun random-elt (choices)
  130.     "Choose an element from a list at random."
  131.     (elt choices (random (length choices))))
  132.  
  133. (defun read-line-no-punct (&aux (input-line (progn (clear-input)(read-line))))
  134.     "Read an input line, ignoring punctuation."
  135.     (read-from-string
  136.         (concatenate 'string "(" 
  137.             (substitute-if #\space #'punctuation-p input-line)
  138.                  ")")))
  139.  
  140. (defun punctuation-p (char) (find char ".,;:`!?#-()\\\""))
  141.  
  142. (defun print-with-spaces (list)
  143.     "Prints a list formatted as a sentence."
  144.     (let ((s (format nil "~{~a ~}" list)))
  145.         (setf (elt s 0) (char-upcase (elt s 0)))
  146.         (unless (punctuation-p (elt s (- (length s) 2)))
  147.             (setf (elt s (1- (length s))) #\.))
  148.         (write s :escape nil)))
  149.  
  150. (defun eliza ()
  151.     "Respond to user input using pattern matching rules."
  152.     (format t "How do you do.  Please state your problem.~%")
  153.     (do* ((input (read-line-no-punct) (read-line-no-punct))
  154.           (response (flatten (use-eliza-rules input))
  155.                     (flatten (use-eliza-rules input))))
  156.         ((or
  157.             (equal input '(good bye))
  158.             (equal input '(good-bye))
  159.             (equal input '(bye))
  160.             (equal input '(quit))
  161.             (equal input '(stop))
  162.             (equal input '(exit)))
  163.             (format t "good bye~%"))
  164.         (print-with-spaces response)
  165.         (terpri)))
  166.  
  167. ;;; ==============================
  168.  
  169. (defparameter *eliza-rules*
  170.  '((((?* ?x) hello (?* ?y))      
  171.     (How do you do.  Please state your problem.))
  172.    (((?* ?x) computer (?* ?y))
  173.     (Do computers worry you?) (What do you think about machines?)
  174.     (Why do you mention computers?)
  175.     (What do you think machines have to do with your problem?))
  176.    (((?* ?x) name (?* ?y))
  177.     (I am not interested in names))
  178.    (((?* ?x) emmett (?* ?y))
  179.     (Emmett is pretty good at Street Fighter |II| |don't| you think?))
  180.    (((?* ?x) sorry (?* ?y))
  181.     (Please don't apologize) (Apologies are not necessary)
  182.     (What feelings do you have when you apologize))
  183.    (((?* ?x) I remember (?* ?y)) 
  184.     (Do you often think of ?y)
  185.     (Does thinking of ?y bring anything else to mind?)
  186.     (What else do you remember) (Why do you recall ?y right now?)
  187.     (What in the present situation reminds you of ?y)
  188.     (What is the connection between me and ?y))
  189.    (((?* ?x) do you remember (?* ?y))
  190.     (Did you think I would forget ?y ?)
  191.     (Why do you think I should recall ?y now)
  192.     (What about ?y) (You mentioned ?y))
  193.    (((?* ?x) if (?* ?y)) 
  194.     (Do you really think its likely that ?y) (Do you wish that ?y)
  195.     (What do you think about ?y) (Really-- if ?y))
  196.  
  197.    (((?* ?x) I dreamt (?* ?y))
  198.     (Really-- ?y) (Have you ever fantasized ?y while you were awake?)
  199.     (Have you dreamt ?y before?))
  200.    (((?* ?x) dream about (?* ?y))
  201.     (How do you feel about ?y in reality?))
  202.    (((?* ?x) dream (?* ?y))    
  203.     (What does this dream suggest to you?) (Do you dream often?)
  204.     (What persons appear in your dreams?)
  205.     (Don't you believe that dream has to do with your problem?))
  206.    (((?* ?x) my mother (?* ?y))
  207.     (Who else in your family ?y) (Tell me more about your family))
  208.    (((?* ?x) my father (?* ?y))
  209.     (Your father) (Does he influence you strongly?) 
  210.     (What else comes to mind when you think of your father?))
  211.  
  212.    (((?* ?x) I want (?* ?y))     
  213.     (What would it mean if you got ?y)
  214.     (Why do you want ?y) (Suppose you got ?y soon))
  215.    (((?* ?x) I am glad (?* ?y))
  216.     (How have I helped you to be ?y) (What makes you happy just now)
  217.     (Can you explain why you are suddenly ?y))
  218.    (((?* ?x) I am sad (?* ?y))
  219.     (I am sorry to hear you are depressed)
  220.     (I'm sure its not pleasant to be sad))
  221.    (((?* ?x) are like (?* ?y))   
  222.     (What resemblance do you see between ?x and ?y))
  223.    (((?* ?x) is like (?* ?y))    
  224.     (In what way is it that ?x is like ?y)
  225.     (What resemblance do you see?)
  226.     (Could there really be some connection?) (How?))
  227.    (((?* ?x) alike (?* ?y))      
  228.     (In what way?) (What similarities are there?))
  229.    (((?* ?x) same (?* ?y))       
  230.     (What other connections do you see?))
  231.  
  232.    (((?* ?x) I was (?* ?y))       
  233.     (Were you really?) (Perhaps I already knew you were ?y)
  234.     (Why do you tell me you were ?y now?))
  235.    (((?* ?x) was I (?* ?y))
  236.     (What if you were ?y ?) (Do you thin you were ?y)
  237.     (What would it mean if you were ?y))
  238.    (((?* ?x) I am (?* ?y))       
  239.     (In what way are you ?y) (Do you want to be ?y ?))
  240.    (((?* ?x) am I (?* ?y))
  241.     (Do you believe you are ?y) (Would you want to be ?y)
  242.     (You wish I would tell you you are ?y)
  243.     (What would it mean if you were ?y))
  244.    (((?* ?x) am (?* ?y))
  245.     (Why do you say "AM?") (I don't understand that))
  246.    (((?* ?x) are you (?* ?y))
  247.     (Why are you interested in whether I am ?y or not?)
  248.     (Would you prefer if I weren't ?y)
  249.     (Perhaps I am ?y in your fantasies))
  250.    (((?* ?x) you are (?* ?y))   
  251.     (What makes you think I am ?y ?))
  252.  
  253.    (((?* ?x) because (?* ?y))
  254.     (Is that the real reason?) (What other reasons might there be?)
  255.     (Does that reason seem to explain anything else?))
  256.    (((?* ?x) were you (?* ?y))
  257.     (Perhaps I was ?y) (What do you think?) (What if I had been ?y))
  258.    (((?* ?x) I can't (?* ?y))    
  259.     (Maybe you could ?y now) (What if you could ?y ?))
  260.    (((?* ?x) I feel (?* ?y))     
  261.     (Do you often feel ?y ?))
  262.    (((?* ?x) I felt (?* ?y))     
  263.     (What other feelings do you have?))
  264.    (((?* ?x) I (?* ?y) you (?* ?z))   
  265.     (Perhaps in your fantasy we ?y each other))
  266.    (((?* ?x) why don't you (?* ?y))
  267.     (Should you ?y yourself?)
  268.     (Do you believe I don't ?y) (Perhaps I will ?y in good time))
  269.    (((?* ?x) yes (?* ?y))
  270.     (You seem quite positive) (You are sure) (I understand))
  271.    (((?* ?x) no (?* ?y))
  272.     (Why not?) (You are being a bit negative)
  273.     (Are you saying "NO" just to be negative?))
  274.  
  275.    (((?* ?x) someone (?* ?y))
  276.     (Can you be more specific?))
  277.    (((?* ?x) everyone (?* ?y))
  278.     (surely not everyone) (Can you think of anyone in particular?)
  279.     (Who for example?) (You are thinking of a special person))
  280.    (((?* ?x) always (?* ?y))
  281.     (Can you think of a specific example) (When?)
  282.     (What incident are you thinking of?) (Really-- always))
  283.    (((?* ?x) what (?* ?y))
  284.     (Why do you ask?) (Does that question interest you?)
  285.     (What is it you really want to know?) (What do you think?)
  286.     (What comes to your mind when you ask that?))
  287.    (((?* ?x) perhaps (?* ?y))    
  288.     (You do not seem quite certain))
  289.    (((?* ?x) are (?* ?y))
  290.     (Did you think they might not be ?y)
  291.     (Possibly they are ?y))
  292.    (((?* ?x))               
  293.     (Very interesting) (I am not sure I understand you fully)
  294.     (What does that suggest to you?) (Please continue) (Go on) 
  295.     (Do you feel strongly about discussing such things?))))
  296.  
  297. ;;; ==============================
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.